﻿using ASP;
using System;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class OptReminder : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }
    

    private void GetReminderSp()
    {
        ReminderDB objDB = new ReminderDB();
        DataTable dt;

        string strSort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

        int nUserId = int.Parse(this.Session[SysUtil.m_str_session_userid].ToString());
        int nAlertDay = int.Parse(this.Session[SysUtil.m_str_session_useralertday].ToString());
        dt = objDB.getReminderSp(nUserId, nAlertDay);
        dt.DefaultView.Sort = strSort;
        GridView1.DataSource = dt;
        GridView1.DataBind();

        objDB.CloseDB();
    }


    private void GetReminderSp2()
    {
        ReminderDB objDB = new ReminderDB();
        DataTable dt;

        string strSort = (string)ViewState["SortOrder2"] + " " + (string)ViewState["OrderDire2"];

        string LoginName = this.Session[SysUtil.m_str_session_loginname].ToString();
        dt = objDB.getReminderSp2(LoginName);
        dt.DefaultView.Sort = strSort;
        GridView2.DataSource = dt;
        GridView2.DataBind();

        objDB.CloseDB();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.ProjectReminder.ToString();
            ViewState["SortOrder"] = "ACMEDueDate";
            ViewState["OrderDire"] = "ASC";
            ViewState["SortOrder2"] = "ACMEDueDate";
            ViewState["OrderDire2"] = "ASC";

            GetReminderSp();
            GetReminderSp2();
        }
    }


    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                        image.ImageUrl = "";
                        if (ViewState["SortOrder"].ToString() == button.CommandArgument.ToString())
                        {
                            if (ViewState["OrderDire"].ToString() == "ASC")
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string projectGroupId = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["ProjectGroupId"].ToString();
        string projectCode = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["ProjectCode"].ToString();
        string projectType = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["Type"].ToString();
        this.Session["fromReminder"] = "1";
        base.Response.Redirect("OptProjectDefault.aspx?projectGroupId=" + projectGroupId.ToString() + "&projectCode=" + projectCode.ToString() + "&projectType=" + projectType.ToString() + "&np=0");
    }


    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                        image.ImageUrl = "";
                        if (ViewState["SortOrder2"].ToString() == button.CommandArgument.ToString())
                        {
                            if (ViewState["OrderDire2"].ToString() == "ASC")
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }


    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string projectGroupId = this.GridView2.DataKeys[this.GridView2.SelectedIndex].Values["ProjectGroupID"].ToString();
        string projectCode = this.GridView2.DataKeys[this.GridView2.SelectedIndex].Values["ProjectCode"].ToString();
        string projectType = this.GridView2.DataKeys[this.GridView2.SelectedIndex].Values["Type"].ToString();
        this.Session["fromReminder"] = "1";
        base.Response.Redirect("OptProjectDefault.aspx?projectGroupId=" + projectGroupId.ToString() + "&projectCode=" + projectCode.ToString() + "&projectType=" + projectType.ToString());
    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "Desc";
            }
        }
        else
        {
            ViewState["SortOrder"] = e.SortExpression;
        }
        GetReminderSp();
    }


    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression;
        if (ViewState["SortOrder2"].ToString() == sPage)
        {
            if (ViewState["OrderDire2"].ToString() == "Desc")
            {
                ViewState["OrderDire2"] = "ASC";
            }
            else
            {
                ViewState["OrderDire2"] = "Desc";
            }
        }
        else
        {
            ViewState["SortOrder2"] = e.SortExpression;
        }
        GetReminderSp2();
    }
}
